<?php
	require ('includes/config.php');
 	$page_title = "amend event";//browser window title will display product name
	include ('includes/header.php');
 

  // Get the update event-->
   
   if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

	// Need the database connection:
	require (MYSQL);
	
	// Trim all the incoming data:
	$trimmed = array_map('trim', $_POST);
	
		// Assume invalid values:
	$e = $m = $s = $t = FALSE;
	
		// Check for a event name:
	if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['ev_name'])) {
		$e = mysqli_real_escape_string ($dbc, $trimmed['ev_name']);
	} else {
		echo '<p class="error">Please enter your event name!</li>';
	}
	

	//Check for the manager:
	if (preg_match ('/^[0-9]+$/',$trimmed['manager'])) {
		$m = mysqli_real_escape_string ($dbc, $trimmed['manager']);
	} else {
		echo '<p class="error">Please enter a valid manager!</li>';
	}
	
		//Check for the date:
	if (preg_match ('/^((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3}))[-:\\/.](?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][01]{1})))(?![\\d])(\\s+)((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)/',$trimmed['date'])) {
		$s = mysqli_real_escape_string ($dbc, $trimmed['date']);
	} else {
		echo '<p class="error">Please enter a valid date number!</li>';
	}
	
	
	$t =(!empty($_POST['type'])) ? trim($_POST['type']) : NULL;
	
	$et = (!empty($_POST['event'])) ? trim($_POST['event']) : NULL;
	
	
	if(	$e && $m && $s && $t && $et ){
				
				$q = "UPDATE events SET  `text`= '$e', `id`= '$m', `type`= '$t', start_date = '$s' WHERE  `ev_id`= '$et'";
				$r = mysqli_query ($dbc, $q);
				
				if (mysqli_affected_rows($dbc) == 1) {
					
					header("Location: events.php");
				
				}
				else{
					echo"sorry we could not update your record";
					}
	
			}
	

   }
  
    ?>
 <!-- fluid body container -->

<div class="container-fluid">
<!--fluid sidebar 3 columns   -->
	<div class="span3">
		<div class="well sidebar-nav">
<?php
require ('includes/side_nav_events.php');
?>
         </div>
    </div>
   <!---end of sidebar 3 columns  -->
  
  <div class="span9">
  		<div class="hero-unit">
	<h2>Amend your event</h2>
    <p>Please complete your changes and save</p>
  
  <?php
   $mid = 24;
		$row = FALSE; // Assume nothing!
  
 			if (isset($_GET['eid']) && filter_var($_GET['eid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) { // Make sure there's a event ID!
  
		        $id = $_GET['eid'];
				
 			if (isset($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {				
				$mid = $_GET['id'];}

			 require(MYSQL);  //open database
   

   
   $q="SELECT `type`,`text`,`end_date`,`start_date`,`ev_id`,events.id,CONCAT_WS(' ', ev_manager.f_name,ev_manager.s_name) FROM `events`,ev_manager WHERE ev_id = $id AND ev_manager.id = $mid";//to print out all events
	$r = mysqli_query($dbc, $q);
	
	if (mysqli_num_rows($r) == 1) { // one result picked
				
						$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

//lays out table
?>
     <form action= "ammend.php" method ="post" name="update">
 <?php    


	
	
	$r = mysqli_query ($dbc, $q);
	
			echo"
					<p><b>Event Name</p>
			<p><input type=\"text\" name= \"ev_name\" value = \"{$row['text']}\"/></p>

					
								<p><b>Manager</b></p>								
								<ul><li>
								<select id =\"manager\"  name=\"manager\">";
								echo"<option value=\"{$row['id']}\">{$row["CONCAT_WS(' ', ev_manager.f_name,ev_manager.s_name)"]}</option>";
								
									$q="SELECT id, CONCAT_WS(' ',f_name,s_name) FROM `ev_manager` ORDER BY s_name ASC";
									$r=mysqli_query($dbc,$q);			
								
									while(list($manager,$name)=mysqli_fetch_array($r,MYSQLI_NUM)){
									echo"<option value=\"$manager\">$name</option>";}
									//mysqli_free_result($r);
									echo "</select></li></ul>
									
								<p>Date</p>	
					
								<p><input type=\"text\" name= \"date\" value = \"{$row['start_date']}\"/></p>
								<p><b>Type</b></p>
					
							   <ul><li>
								<select id = type name=type>";
								echo"<option value=\"{$row['type']}\">{$row['type']}</option>";
								
									$q='SELECT `type` FROM `type` ORDER BY `type` ASC';
									$r=mysqli_query($dbc,$q);			
								
									while(list($type)=mysqli_fetch_array($r,MYSQLI_NUM)){
									echo"<option value=\"$type\">$type</option>";}
									//mysqli_free_result($r);
									echo "</select></li></ul>
			
			<input type='hidden' name='event' value='$id'>

			<p><input type=\"submit\" value=\"update\"/></p>
		</form>";
	}
	   

 }
 ?>
 
 <a href="Events.php">Back to Events</a>
 		 </div>
  <!--end of hero-unit -->
  <!--start of events list -->
  		
    </div>
  <!--end of events list -->
<!-- start of left bar to do list -->
 	
    </div>
    <!-- end of to do list-->
    </div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/bootstrap.min.js"></script>
	<script src="js/bootstrap.js"></script>
  </body>
</html>
<?php ob_flush(); ?>

			





